import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { Location } from '@angular/common';
// 服务
import { LoadingAnimateService } from '../../../common/loading';
import { EventService } from './event.service';

import { EventList, EventResult, EventHookResult, PageObj } from './event';
import { AlertService } from '../../../common/alert';
@Component({
    moduleId: module.id,
    templateUrl: 'event.component.html',
    providers: [EventService]
})
export class EventComponent implements OnInit {

    etype: string;
    action: string;
    previd: string;
    isNull: number;
    from: string;
    pageObj: PageObj = new PageObj();
    datas: EventResult[];
    isNextpage: boolean;
    isPrevpage: boolean;
    currentPage: number;

    constructor(
        private router: Router,
        private route: ActivatedRoute,
        private loadingService: LoadingAnimateService,
        private eventService: EventService,
        private alert: AlertService,
        private location: Location
    ) { }

    ngOnInit(): void {

        this.route.queryParams.subscribe(params => {
            this.etype = params['etype'] ? decodeURI(params['etype']) : '';
            this.action = params['action'] ? decodeURI(params['action']) : '';
            this.from = params['from'] ? decodeURI(params['from']) : '';
            let count = params['count'] ? params['count'] : 10;
            let currentPage = params['page'] ? params['page'] : 1;
            this.find(count, currentPage);
        });

    }

    find(count: number, page: number): void {
        this.loadingService.setValue(true);
        this.eventService.find(count, this.etype, this.action, this.from, page).then(
            res => {
                this.loadingService.setValue(false);
                if (res == null || res.list == null || res.list.length == 0) {
                    this.isNull = 1;
                } else {
                    this.isNull = 0;
                }
             //   console.log("count:" + this.pageObj.count + " page:" + this.pageObj.currentPage)
                this.pageObj.total = res.total;
                this.pageObj.count = count;
                setTimeout(() => {
                    
                 //   console.log("setTimeout count:" + this.pageObj.count + " page:" + this.pageObj.currentPage)
                    this.pageObj.currentPage = page;

                 //   console.log("setTimeout count:" + this.pageObj.count + " page:" + this.pageObj.currentPage)
                }, 10)
               // console.log("count:" + this.pageObj.count + " page:" + this.pageObj.currentPage)
                this.datas = res.list;
            }
        );
    }

    // 搜索
    search(): void {
        let ps = {}
        if (this.etype != "") {
            ps["etype"] = encodeURI(this.etype);
        }
        if (this.action != "") {
            ps["action"] = encodeURI(this.action);
        }
        if (this.from != "") {
            ps["from"] = encodeURI(this.from);
        }

        this.pageObj.currentPage = 1;
        ps["count"] = this.pageObj.count;
        ps["page"] = this.pageObj.currentPage;
        const navigationExtras: NavigationExtras = {
            queryParams: ps
        };

        this.router.navigate(['/client/event'], navigationExtras);
        //this.find(this.pageObj.count,this.pageObj.currentPage);
    }

    pageChanged(): void {
        let ps = {}
        if (this.etype != "") {
            ps["etype"] = encodeURI(this.etype);
        }
        if (this.action != "") {
            ps["action"] = encodeURI(this.action);
        }
        if (this.from != "") {
            ps["from"] = encodeURI(this.from);
        }
        ps["count"] = this.pageObj.count;
        ps["page"] = this.pageObj.currentPage;
        const navigationExtras: NavigationExtras = {
            queryParams: ps
        };
       // this.pageObj.currentPage = $event.page;
        this.router.navigate(['/client/event'], navigationExtras);
        //this.find()
    }
    info(item: EventResult): void {
        console.log(item)
        this.eventService.tmpSave(item)
        this.router.navigate(['/client/event/hook/' + item.eid]);
    }

}
